package br.com.j7.jdesk.ui.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import br.com.j7.jdesk.persistence.entity.Products;
import br.com.j7.jdesk.persistence.entity.Sales;
import br.com.j7.jdesk.persistence.repository.ProductRepository;
import br.com.j7.jdesk.persistence.repository.ProductSaleRepository;
import br.com.j7.jdesk.persistence.repository.SaleRepository;
import br.com.j7.jdesk.persistence.repository.impl.ProductRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ProductSaleRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.SaleRepositoryImpl;

/**
 * 
 * 
 * @author Edivando Alves
 *
 */
@ManagedBean
@SessionScoped
public class SaleBean extends WebContext{
	
	private Products product = new Products();
	private Sales sale = new Sales();
	private List<Products> products;

	private ProductRepository productRepository;
	private SaleRepository saleRepository;
	private ProductSaleRepository productSaleRepository;
	
	public SaleBean() {
		productRepository = new ProductRepositoryImpl();
		productSaleRepository = new ProductSaleRepositoryImpl();
		saleRepository = new SaleRepositoryImpl();
	}

	public String addProduct(){
		try {
			Integer nrQuantity = product.getNrQuantity();
			product = productRepository.findByCode(product.getCdCode(), getSessionEnterprise());
			product.setNrQuantity(nrQuantity);
			products.add( product );
			product = new Products();
			return "sale";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	public String addSale(){
		try {

//			sale.setEnterprise(getSessionEnterprise());
//			sale.setUser(getSessionUser());
//			sale.setCdPayment("Forma Pagamento"); //TODO:
////			sale.setCustomer(customer) TODO:
//			sale.setNrTotalSale(50.00);//TODO:
//			sale = saleRepository.add(sale);
//			
//			for (Products produto : products) {
//				ProductsSales ps = new ProductsSales();
//				ps.setNrQuantity(produto.getNrQuantity());
//				ps.setProduct(produto);
//				ps.setSale(sale);
//				productSaleRepository.add(ps);
//				
//				//Update product
//				Products productDB = productRepository.findByCode(produto.getCdCode(), getSessionEnterprise());
//				productDB.setNrQuantity(productDB.getNrQuantity() - ps.getNrQuantity());
//				productRepository.update(productDB);
//			}
			product = new Products();
			sale = new Sales();
			products = null;
			
			return "sale";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		
	}
	
	public Products getProduct() {
		return product;
	}
	
	public void setProduct(Products product) {
		this.product = product;
	}
	
	public Sales getSale() {
		return sale;
	}
	
	public void setSale(Sales sale) {
		this.sale = sale;
	}
	
	public List<Products> getProducts() {
		if (products == null) {
			products = new ArrayList<>();
		}
		return products;
	}
}
